package com.starnet.yarnmonitor.yarn.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.starnet.yarnmonitor.security.entity.SysUser;
import com.starnet.yarnmonitor.security.mapper.SysUserMapper;
import com.starnet.yarnmonitor.security.utils.UserUtil;
import com.starnet.yarnmonitor.yarn.handler.ApiException;
import com.starnet.yarnmonitor.yarn.vo.YarnWarnOptionVO;
import com.starnet.yarnmonitor.yarn.vo.param.YarnWarnOptionAddParam;
import com.starnet.yarnmonitor.yarn.vo.param.YarnWarnOptionUpdateParam;
import com.starnet.yarnmonitor.yarn.entity.YarnWarnOption;
import com.starnet.yarnmonitor.yarn.mapper.YarnWarnHistoryMapper;
import com.starnet.yarnmonitor.yarn.mapper.YarnWarnOptionMapper;
import com.starnet.yarnmonitor.yarn.service.YarnWarnOptionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.starnet.yarnmonitor.yarn.utils.PageUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 陈佳豪
 * @since 2022-04-24
 */
@Service
public class YarnWarnOptionServiceImpl extends ServiceImpl<YarnWarnOptionMapper, YarnWarnOption> implements YarnWarnOptionService {
    @Autowired
    YarnWarnOptionMapper yarnWarnOptionMapper;
    @Autowired
    YarnWarnHistoryMapper yarnWarnHistoryMapper;
    @Autowired
    SysUserMapper sysUserMapper;


    @Override
    public IPage<YarnWarnOptionVO> getAllOptions(Integer cur, Integer size) {
        IPage<YarnWarnOption> iPage = PageUtils.getIPage(cur, size, YarnWarnOption.class);
        yarnWarnOptionMapper.selectPage(iPage, null);
        List<YarnWarnOption> recordsList = iPage.getRecords();
        List<YarnWarnOptionVO> recordsVOList = new ArrayList<>();
        for (YarnWarnOption yarnWarnOption : recordsList) {
            YarnWarnOptionVO yarnWarnOptionVO = new YarnWarnOptionVO();
            BeanUtils.copyProperties(yarnWarnOption, yarnWarnOptionVO);
            int createUserID = yarnWarnOption.getCreateUser();
            int updateUserID = yarnWarnOption.getUpdateUser();
            String createAccount = sysUserMapper.selectById(createUserID).getAccount();
            //如果跟创建人的id一样，直接赋值，不用再查数据库
            String updateAccount = createUserID == updateUserID ? createAccount : sysUserMapper.selectById(updateUserID).getAccount();
            //如果创建他的账号被移除了，给个提醒
            if (createAccount == null) {
                yarnWarnOptionVO.setCreateAccount("创建者的账号已被删除");
                yarnWarnOptionVO.setUpdateAccount("创建者的账号已被删除");
            } else {
                yarnWarnOptionVO.setCreateAccount(createAccount);
                yarnWarnOptionVO.setUpdateAccount(updateAccount);
            }
            recordsVOList.add(yarnWarnOptionVO);
        }
        IPage<YarnWarnOptionVO> iPageVO = new Page<>();
        BeanUtils.copyProperties(iPage, iPageVO);
        iPageVO.setRecords(recordsVOList);
        return iPageVO;
    }

    @Override
    public void addNewOptions(YarnWarnOptionAddParam yarnWarnOptionAddParam) {
        YarnWarnOption yarnWarnOption = new YarnWarnOption();
        BeanUtils.copyProperties(yarnWarnOptionAddParam, yarnWarnOption);
        Timestamp now = Timestamp.valueOf(LocalDateTime.now());
        yarnWarnOption.setCreateTime(now);
        yarnWarnOption.setUpdateTime(now);

        //当前用户id
        int id = UserUtil.getCurrentUserId();
        //设置创建人和更新人为当前用户
        yarnWarnOption.setCreateUser(id);
        yarnWarnOption.setUpdateUser(id);
        int insert = yarnWarnOptionMapper.insert(yarnWarnOption);
        if (insert != 1) {
            throw new ApiException("新增告警失败");
        }

    }

    @Override
    public void deleteOptionById(Long id) {
        int delete = yarnWarnOptionMapper.deleteById(id);
        if (delete != 1) {
            throw new ApiException("删除失败");
        }
    }

    @Override
    public void updateOption(YarnWarnOptionUpdateParam yarnWarnOptionUpdateParam) {
        YarnWarnOption yarnWarnOption = new YarnWarnOption();
        Timestamp now = Timestamp.valueOf(LocalDateTime.now());
        yarnWarnOption.setUpdateTime(now);
        yarnWarnOption.setUpdateUser(UserUtil.getCurrentUserId());
        BeanUtils.copyProperties(yarnWarnOptionUpdateParam, yarnWarnOption);
        int i = yarnWarnOptionMapper.updateById(yarnWarnOption);
        if (i == 0) {
            throw new ApiException("更新告警阈值失败");
        }
    }
}
